// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DepthImageComparisonModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.Visualization
{
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision.Cameras;
    using Microsoft.Robotics.Vision.Runtime.Cameras;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// This model agent class represents two depth frames to be compared side by side
    /// </summary>
    [DataContract]
    public sealed class DepthImageComparisonModelAgent : ModelAgent<DepthCameraAgentMessage<KinectDepthCameraParameters>, DepthImageComparisonModel>, 
                                                         ISubscriptionReceiver<RightKinectAgentMessage>
    {
        /// <summary>
        /// Name of the agent producing the image to be displayed on the right
        /// </summary>
        [DataMember(Name = "RightImageProducer")]
        private string rightImageProducer = default(string);

        /// <summary>
        /// Agent providing right image.
        /// </summary>
        private IAgentHost rightImageProducerAgent;

        /// <summary>
        /// Initializes a new instance of the DepthImageComparisonModelAgent class
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producerAgentName">The name of the agent we subscribe to.</param>
        /// <param name="maxUpdateIntervalMilliseconds">The interval on which to issue data binding updates to the UI</param>
        /// <param name="rightImageProducer">Name of the agent producing the image to be displayed on the right</param>
        public DepthImageComparisonModelAgent(
            string name, string producerAgentName, int maxUpdateIntervalMilliseconds, string rightImageProducer)
            : base(name, producerAgentName, maxUpdateIntervalMilliseconds)
        {
            this.rightImageProducer = rightImageProducer;
        }

        /// <summary>
        /// Initialize the agent
        /// </summary>
        /// <param name="locator">The agent locator to use</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);

            if (this.rightImageProducer != default(string))
            {
                this.rightImageProducerAgent = this.Locator.GetAgent(this.rightImageProducer);
            }
        }

        /// <summary>
        /// Called when the agent is activated
        /// </summary>
        public override void OnActivated()
        {
            base.OnActivated();

            if (this.rightImageProducerAgent != null)
            {
                this.rightImageProducerAgent.Subscribe<RightKinectAgentMessage>(this);
                this.rightImageProducerAgent.Activate();
            }
        }

        /// <summary>
        /// Update the right image in the model
        /// </summary>
        /// <param name="rightMessage">A kinect depth message with a modified type so we can distinguish it from
        /// the message for the left hand image.</param>
        public void Receive(RightKinectAgentMessage rightMessage)
        {
            ushort[] image =
                DepthImageVisualizationUtilities.Prepare16BitDepthImageFromKinectAgentMessage(rightMessage.Message);

            this.Model.VideoFrameRight = new VideoFrame()
                {
                    Data = image,
                    Size = new System.Drawing.Size(rightMessage.Message.ImageFrame.Width, rightMessage.Message.ImageFrame.Height),
                    Format = SimplePixelFormat.Gray16,
                    Stride = rightMessage.Message.ImageFrame.Width * 2
                };
        }

        /// <summary>
        /// Updates the model data with the new state.
        /// </summary>
        /// <param name="state">The new state from the upstream agent.</param>
        /// <returns>true if the model was updated, false otherwise</returns>
        protected override bool UpdateModel(DepthCameraAgentMessage<KinectDepthCameraParameters> state)
        {
            ushort[] image = DepthImageVisualizationUtilities.Prepare16BitDepthImageFromKinectAgentMessage(state);

            this.Model.VideoFrameLeft = new VideoFrame()
                {
                    Data = image,
                    Size = new System.Drawing.Size(state.ImageFrame.Width, state.ImageFrame.Height),
                    Format = SimplePixelFormat.Gray16,
                    Stride = state.ImageFrame.Width * 2
                };
            return true;
        }
    }
}
